package com.jeesite.modules.exam.score.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.exam.assessment.entity.DcSupExamInfo;
import com.jeesite.modules.exam.notice.entity.DcSupExamNotice;
import com.jeesite.modules.exam.notice.service.DcSupExamNoticeService;
import com.jeesite.modules.exam.quota.entity.DcSupExamSumQuotaInfo;
import com.jeesite.modules.exam.score.vo.DcSupExamQuotaOrgVo;
import com.jeesite.modules.exam.score.entity.DcSupExamScore;
import com.jeesite.modules.exam.score.service.DcSupExamScoreService;
import com.jeesite.modules.exam.score.vo.DcSupExamVo;
import com.jeesite.modules.sys.utils.UserUtils;
import com.jeesite.modules.utils.OfficeUtil;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Optional;

/**
 * 关键指标评测
 */
@Controller
@RequestMapping("${adminPath}/score/dcSupExamGJZBScore")
@RequiredArgsConstructor
public class DcSupExamGJZBScoreController extends BaseController {

    private final DcSupExamNoticeService dcSupExamNoticeService;

    private final DcSupExamScoreService dcSupExamScoreService;


    /**
     * 关键指标评分入口
     * @return
     */
    @RequiresPermissions("score:dcSupGjScore:view")
    @RequestMapping("gjIndexView")
    public String gjIndex(DcSupExamInfo dcSupExamInfo, Model model){
        Integer currentYear = Integer.parseInt(DateUtils.getYear());
        model.addAttribute("yearList", OfficeUtil.getYearList(currentYear));
        model.addAttribute("currentYear", currentYear);
        model.addAttribute("dcSupExamInfo", dcSupExamInfo);
        return "modules/exam/score/dcSupExamGJScoreList";
    }

    /**
     * 评分入口数据
     */
    @RequiresPermissions("score:dcSupGjScore:view")
    @ResponseBody
    @PostMapping("gjIndexListData")
    public Page<DcSupExamInfo> gjIndexListData(DcSupExamInfo dcSupExamInfo, HttpServletRequest request, HttpServletResponse response){
        dcSupExamInfo.setPage(new Page<>(request, response));
        dcSupExamInfo.setExamStatus("2");
        return dcSupExamScoreService.gjIndexListData(dcSupExamInfo);
    }


    /**
     * 关键指标页面
     */
    @RequiresPermissions("score:dcSupGjScore:view")
    @RequestMapping("view")
    public String indexView(DcSupExamNotice notice,String selectExamOrg, Model model){
        List<DcSupExamQuotaOrgVo> assessmentUnit = dcSupExamScoreService.findAssessmentUnit(notice.getExamId(),"1"); //1 关键指标
        model.addAttribute("assessmentUnit",assessmentUnit);
        model.addAttribute("examId",notice.getExamId());
        model.addAttribute("selectExamOrg",selectExamOrg);
        return "modules/exam/score/dcSupExamGJZBScore";
    }

    @RequiresPermissions("score:dcSupGjScore:view")
    @RequestMapping("subView")
    public String subView(@RequestParam("examId") String examId, @RequestParam("exameOrg")String exameOrg ,Model model){
        List<DcSupExamSumQuotaInfo> gjQuotaScoreList = dcSupExamScoreService.findGJListData(examId,exameOrg);
        DcSupExamScore dcSupExamScoreVo = new DcSupExamScore();
        dcSupExamScoreVo.setExamId(examId);
        dcSupExamScoreVo.setScoreType("2");
        dcSupExamScoreVo.setExameOrg(exameOrg);
        DcSupExamScore dcSupExamScore = dcSupExamScoreService.findDcSupExamScore(dcSupExamScoreVo);//获取关键指标信息
        DcSupExamVo dcSupExamVo = new DcSupExamVo();
        dcSupExamVo.setDcSupExamScore(Optional.ofNullable(dcSupExamScore).orElseGet(DcSupExamScore::new));
        dcSupExamVo.setDcSupScoreList(gjQuotaScoreList);
        dcSupExamVo.setExameOrg(exameOrg);
        dcSupExamVo.setExamId(examId);
        model.addAttribute("dcSupExamVo",dcSupExamVo);
        return "modules/exam/score/dcSupExamGJZBSubScore";
    }

    /**
     * 保存关键指标分数 并且汇总
     */
    @RequiresPermissions("score:dcSupGjScore:view")
    @PostMapping("/saveGJScore")
    @ResponseBody
    public String saveGJScore(DcSupExamVo dcSupExamVo){
        if (UserUtils.getUser().getUserCode().equals("system")) {
            return renderResult(Global.FALSE,"操作禁止！");
        }
        dcSupExamScoreService.saveGJScore(dcSupExamVo);
        return renderResult(Global.TRUE,"保存成功！");
    }

    /**
     * 撤回关键指标分数
     */
    @RequiresPermissions("score:dcSupGjScore:view")
    @PostMapping("/withDrawGJScore")
    @ResponseBody
    public String withDrawGJScore(String examId,String exameOrg){
        if (UserUtils.getUser().getUserCode().equals("system")) {
            return renderResult(Global.FALSE,"操作禁止！");
        }
        try {
            dcSupExamScoreService.withDrawGJScore( examId, exameOrg);
        } catch (Exception e) {
            return renderResult(Global.FALSE,e.getMessage());
        }
        return renderResult(Global.TRUE,"撤回成功！");
    }


}
